// 计算属性相关功能
import { computed, ComputedRef } from 'vue';
import type { Message } from './chat-types';
import { filterMessages } from './chat-message-filter';

/**
 * 创建当前消息的计算属性
 */
export function createCurrentMessages(
  messages: { value: Message[] },
  currentTargetId: { value: string | null },
  targetType: { value: 'User' | 'Group' },
  currentUserId: { value: string | null }
): ComputedRef<Message[]> {
  return computed(() => {
    if (!currentTargetId.value) {
      if (import.meta.env.DEV || import.meta.env.MODE === 'development') {
        console.log('⚠️ currentMessages: 没有选择聊天目标，返回空数组', {
          totalMessages: messages.value.length,
        });
      }
      return [];
    }

    const targetId = String(currentTargetId.value);
    const currentUserIdStr = String(currentUserId.value || '');
    
    // 使用拆分后的过滤函数
    const filtered = filterMessages(
      messages.value,
      targetId,
      targetType.value,
      currentUserIdStr
    );
    
    // 调试日志（只在开发模式或过滤结果异常时输出）
    if (import.meta.env.DEV || import.meta.env.MODE === 'development' || filtered.length === 0 && messages.value.length > 0) {
      // 获取与目标相关的所有消息ID（用于调试）
      const relatedMessages = messages.value.filter(msg => {
        const msgFromId = String(msg.from_user_id || '').trim();
        const msgToId = String(msg.to_user_id || '').trim();
        return msgFromId === targetId || msgToId === targetId ||
               msgFromId.toLowerCase() === targetId.toLowerCase() ||
               msgToId.toLowerCase() === targetId.toLowerCase();
      });
      
      console.log('📋 currentMessages 计算结果:', {
        targetId,
        totalMessages: messages.value.length,
        filteredCount: filtered.length,
        relatedMessagesCount: relatedMessages.length,
        targetType: targetType.value,
        currentUserId: currentUserIdStr,
        warning: filtered.length === 0 && messages.value.length > 0 
          ? '⚠️ 有消息但被全部过滤，可能是ID不匹配' 
          : undefined,
        sampleAllMessages: messages.value.slice(0, 5).map(m => ({
          message_id: m.message_id,
          from_user_id: m.from_user_id,
          to_user_id: m.to_user_id,
          isReceived: m.isReceived,
          chat_type: m.chat_type,
        })),
        sampleFilteredMessages: filtered.slice(0, 3).map(m => ({
          message_id: m.message_id,
          from_user_id: m.from_user_id,
          to_user_id: m.to_user_id,
          isReceived: m.isReceived,
          chat_type: m.chat_type,
        })),
        sampleRelatedMessages: relatedMessages.slice(0, 3).map(m => ({
          message_id: m.message_id,
          from_user_id: m.from_user_id,
          to_user_id: m.to_user_id,
          isReceived: m.isReceived,
          chat_type: m.chat_type,
        })),
      });
      
      // 如果过滤结果为空但有相关消息，输出更详细的调试信息
      if (filtered.length === 0 && relatedMessages.length > 0) {
        console.warn('⚠️ 发现相关消息但被过滤，可能是ID格式不匹配:', {
          targetId,
          relatedMessageIds: relatedMessages.map(m => ({
            from_user_id: m.from_user_id,
            to_user_id: m.to_user_id,
            isReceived: m.isReceived,
          })),
        });
      }
    }
    
    return filtered;
  });
}

